package com.example.mansystem.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.mansystem.dto.DetailDto;
import com.example.mansystem.dto.DictDto;
import com.example.mansystem.entity.CallResultMsg;
import com.example.mansystem.entity.DictDO;
import com.example.mansystem.entity.DictDetailDO;
import com.example.mansystem.exception.UniformReponseHandler;
import com.example.mansystem.mapper.DictDetailMapper;
import com.example.mansystem.mapper.DictMapper;
import com.example.mansystem.service.DictService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/dict")
public class DictController {
    @Autowired
    DictDetailMapper dictDetailMapper;
    @Autowired
    DictMapper dictMapper;
    @Autowired
    DictService dictService;
    @GetMapping("/getInstrumentDict")
    public CallResultMsg getInstrumentDict()
    {
        List<Map<String,String>> res= dictService.getInstrumentCardDict();
        return new UniformReponseHandler<List<Map<String,String>>>().sendSuccessResponse(res);
    }

    @GetMapping("/getAllDict")
    public CallResultMsg getAllDict()
    {
        List<DictDO> res=dictMapper.selectList(null);
        return new UniformReponseHandler<List<DictDO>>().sendSuccessResponse(res);
    }

    @GetMapping("/getDictDetail/{id}")
    public CallResultMsg getDictDetail(@PathVariable(value = "id") String id)
    {
        int Id = Integer.parseInt(id);
        LambdaQueryWrapper<DictDetailDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DictDetailDO::getDictId,Id);
        List<DictDetailDO> list = dictDetailMapper.selectList(wrapper);
        return new UniformReponseHandler<List<DictDetailDO>>().sendSuccessResponse(list);
    }

    @GetMapping("/delete/{id}")
    public CallResultMsg deleteDict(@PathVariable(value = "id") String id)
    {
        int Id = Integer.parseInt(id);

        dictMapper.deleteById(Id);
        return new UniformReponseHandler<String>().sendSuccessResponse("SUCCESS");
    }

    @GetMapping("/deleteDetail/{id}")
    public CallResultMsg deleteDetail(@PathVariable(value = "id") String id)
    {
        int Id = Integer.parseInt(id);

        dictDetailMapper.deleteById(Id);
        return new UniformReponseHandler<String>().sendSuccessResponse("SUCCESS");
    }
    @PostMapping("/ChangeDict")
    public CallResultMsg ChangeDict(@RequestBody DictDto dto)
    {
        if(dto.getType()==1)
        {
            DictDO dictDO = dictMapper.selectById(dto.getId());
            dictDO.setCode(dto.getCode());
            dictDO.setDescript(dto.getDescript());
            dictDO.setName(dto.getName());
            dictMapper.updateById(dictDO);
        }
        else
        {
            DictDO dictDO = DictDO.builder().code(dto.getCode()).descript(dto.getDescript())
                    .name(dto.getName()).status(1)
                    .build();
            dictMapper.insert(dictDO);
        }
        return new UniformReponseHandler<String>().sendSuccessResponse("SUCCESS");
    }

    @PostMapping("/ChangeDetail")
    public CallResultMsg ChangeDetail(@RequestBody DetailDto dto)
    {
        if(dto.getType()==1)
        {
            DictDetailDO dictDetailDO = dictDetailMapper.selectById(dto.getId());
            dictDetailDO.setCode(dto.getCode());
            dictDetailDO.setName(dto.getName());
            dictDetailDO.setSort(dto.getSort());
            dictDetailMapper.updateById(dictDetailDO);
        }
        else
        {
            DictDetailDO dictDetailDO = DictDetailDO.builder()
                    .dictId(dto.getDictId())
                    .code(dto.getCode())
                    .sort(dto.getSort())
                    .name(dto.getName())
                    .build();
            dictDetailMapper.insert(dictDetailDO);
        }
        return new UniformReponseHandler<String>().sendSuccessResponse("SUCCESS");
    }


}
